//
//  DsErYiViewController.m
//  Tour
//
//  Created by 张张烨 on 16/3/9.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "DsErYiViewController.h"
#import "PrefixHeader.pch"
#import "DsErYiCell.h"
#import "DsErYiModel.h"
#import "DsCellVC.h"
#import "DsTouxiangVC.h"
@interface DsErYiViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,dsPassDelegate>
@property (nonatomic, assign)NSInteger i;
@property (nonatomic, retain)BaseTableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;

@end

@implementation DsErYiViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
//    self.navigationController.navigationBar.alpha = 1;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tuijian"]];
    self.dataArr = [NSMutableArray array];
    self.i = 1;
    // tableView 数据处理
    [self refresh];
    
    // tableView创建
    [self creartTableView];
    [self shuaxinAndJiazai];
   
    

    
}




- (void)refresh
{
    
    
    
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"http://chanyouji.com/api/trips/featured.json?page=1"] completion:^(id result) {
        NSString *arrPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"huancun.plist"];
        [result writeToFile:arrPath atomically:YES];
        
        

        NSArray *arr = result;
        self.dataArr = [DsErYiModel modelHanderWithArray:arr];
        // 刷新
        [_tableView reloadData];
    }];
}




- (void)creartTableView
{
    // 初始化
    self.tableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGH-108) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 隐藏滚动条
    _tableView.showsVerticalScrollIndicator = NO;
    //  注册重用池
    [_tableView registerClass:[DsErYiCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
   
}
-(void)shuaxinAndJiazai
{
    
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        // 刷新
        //        self.isSj = NO;
        [self refresh];
        
        //新版本是 MJ_header
        [_tableView.mj_header endRefreshing];
        
    }];
    //设置刷新控件
    self.tableView.mj_header = header;
    
    /**<  加载 */
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        _i++;
        
        [AFNModel GETWithHandler:[NSString stringWithFormat:@"http://chanyouji.com/api/trips/featured.json?page=%ld",_i] completion:^(id result) {
            
            
            NSArray *arr = result;
            //            self.dataArr = [DsErYiModel modelHanderWithArray:arr];
            for (NSDictionary *dic in arr) {
                
                
                DsErYiModel *ds = [DsErYiModel modelWithDic:dic];
                [self.dataArr addObject:ds];
                
            }
            
            [_tableView reloadData];
        }];
        
        
        
        
        [_tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
}
#pragma mark - 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DsErYiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.model = _dataArr[indexPath.row];
    
    cell.delagate = self;
    
    return cell;
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 250 * GAO ;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.hidesBottomBarWhenPushed = YES;
    DsCellVC *next = [[DsCellVC alloc]init];
    DsErYiModel *m = _dataArr[indexPath.row];
    next.userid = [m.cid stringValue];
    next.touxiang = m.image;
    [self.navigationController pushViewController:next animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
/**<  砍了 */
#warning mark -- 上线需要注掉这个方法
- (void)passtiaotiao:(NSNumber *)tiao
{
//    self.hidesBottomBarWhenPushed=YES;
//    DsTouxiangVC *next = [[DsTouxiangVC alloc]init];
//    next.userid = [tiao stringValue];
//    [self.navigationController pushViewController:next animated:YES];
//    self.hidesBottomBarWhenPushed = NO;
}


/**<  背景图片滚动动画 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    NSArray<DsErYiCell *> *array = [self.tableView visibleCells];
    [array enumerateObjectsUsingBlock:^(DsErYiCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj cellOffset];
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
